import { observable, action } from 'mobx';
import api from '../apis/app';

export default class SetGroupNoWrite {
  @observable conversationId = '';
  @observable pkId = '';
  @observable filterGroupPerson = [];
  @observable groupPerson = [];
  @observable noWritePerson = [];
  @observable hasInit = true;

  @action init = (pkId, conversationId) => {
    this.pkId = pkId;
    this.conversationId = conversationId;
    this.hasInit = false;

    Promise.all([
      api.getConversationInfo({
        conversationId: conversationId,
        conversationType: 1,
        detail: 1
      }),
      api.getExceptMembers({
        pkId: pkId
      })
    ]).then(([person, noWritePerson]) => {
      this.groupPerson = person.members;
      this.filterGroupPerson = person.members;
      this.noWritePerson = noWritePerson;
      this.hasInit = true;
    });
  }

  @action onPersonChange = selectedRowKeysAllPages => {
    this.noWritePerson = selectedRowKeysAllPages;
  }

  @action onSearch = value => {
    this.filterGroupPerson = this.groupPerson.filter(person => !!~person.lastname.indexOf(value));
  }

  @action onSure = () => {
    return new Promise((resolve, reject) => {
      api.modifyExceptMembers({
        pkId: this.pkId,
        userIds: this.noWritePerson.join(',')
      }).then(res => {
        resolve();
      })
    });
  }

}